/**
 * @description 用户查询参数校验
 */
export const userQuerySchema = {
  type: 'object',
  properties: {
    deptId: { type: 'string', description: '部门ID', nullable: true },
    username: { type: 'string', description: '用户名', nullable: true },
    mobile: { type: 'string', description: '手机号', nullable: true },
    status: { type: 'number', description: '状态：0-正常，1-停用', nullable: true },
    createTime: { 
      type: 'array',
      items: { type: 'string' },
      description: '创建时间范围',
      nullable: true 
    },
    pageNo: { type: 'number', description: '页码', default: 1 },
    pageSize: { type: 'number', description: '每页条数', default: 10 },
  },
  required: ['pageNo', 'pageSize'], // 默认值已设置为1和10
}
/**
 * @description 财务查询参数校验
 */
export const financeQuerySchema = {
  type: 'object',
  properties: {
    deptName: { type: 'string', description: '部门名称', nullable: true },
    employeeName: { type: 'string', description: '员工姓名', nullable: true },
    accountName: { type: 'string', description: '账户名称', nullable: true },
    accountType: { type: 'string', description: '账户类型', nullable:true },
    amount: { type: 'string', description: '金额', nullable: true },
    explanation: { type: 'string', description: '说明', nullable: true },
    date: { 
      type: 'array',
      items: { type: 'string' },
      description: '日期范围',
      nullable: true 
    },
    appId: { type: 'string', description: '应用ID', nullable: true },
  },
  required: ['pageNo', 'pageSize'],
}

export const processListQuerySchema = {
  name: "processList",
  description: "获取换届流程列表接口",
  inputSchema: {
    type: 'object',
    properties: {
      processType: { type: 'number', description: '换届类型', nullable: true, default: 1 },
      isPublic: { type: 'number', description: '是否公开', nullable: true, default: 0 },
      organId: { type: 'number', description: '组织ID', nullable: true, default: 3306 },
      organType: { type: 'number', description: '组织类型', nullable: true, default: 3 },
      createTime: { 
        type: 'array',
        items: { type: 'string' },
        description: '创建时间范围',
        nullable: true,
      }
    },
    required: []
  }
}

// 将JSONSchema转换为MCP Schema，需要将inputSchema.inputSchema下面的字段
// description: '换届类型', nullable: true, default: 1重新组装到工具的描述中description
// 这个描述是为了让大模型更好的了解工具的输入参数以及每个参数的含义
export const convertJsonSchemaToMcpSchema = (jsonSchema: any) => {
  console.log('Processing schema:', JSON.stringify(jsonSchema, null, 2));
  if (!jsonSchema || !jsonSchema.inputSchema || !jsonSchema.inputSchema.properties) {
    console.warn('Invalid schema structure:', jsonSchema);
    return jsonSchema;
  }
  let description = (jsonSchema.description || '') + ',参数描述:';
  Object.entries(jsonSchema.inputSchema.properties).forEach(([name, item]: [string, any]) => {
    if (item && typeof item === 'object') {
      description += `字段名称:${name},类型:${item.type || 'unknown'},描述:${item.description || ''},是否可选:${item.nullable ? '可选' : '必填'},默认值:${item.default !== undefined ? item.default : '无'} `;
      if (item.enum) {
        description += `,枚举值:${item.enum.join(',')}`;
      }
      description += ',';
    } else {
      console.warn('Invalid property:', name, item);
    }
  });
  jsonSchema.description = description;
  return jsonSchema;
}

export const convertJsonSchemasToMcpSchema = (jsonSchemas: any[]) => {
  console.log('Processing schemas array:', jsonSchemas?.length);
  if (!Array.isArray(jsonSchemas)) {
    console.warn('Input is not an array:', jsonSchemas);
    return [];
  }
  return jsonSchemas.map(jsonSchema => convertJsonSchemaToMcpSchema(jsonSchema)).filter(Boolean);
}
